﻿Option Strict On
Option Explicit On 

Imports System.Windows.forms


''' -----------------------------------------------------------------------------
''' Project	 : IPDataControl
''' Class	 : Core.IPDataControl.CDEFormStateControl
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Control state của DE form bao gồm cả các control nếu cần
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	3/15/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CDEFormStateControl
    Inherits CDataEntryFormButtonsStateControl
#Region "MEMBERS"
    Private m_array_of_controls As New ArrayList
    Private m_stb As StatusBar
#End Region

#Region "CONSTANTS"
    Const C_UPDATE_STATUS_STRING As String = "SỬA"
    Const C_INSERT_STATUS_STRING As String = "THÊM MỚI"
    Const C_VIEW_STATUS_STRING As String = "XEM"
#End Region

#Region "PUBLIC"
    Public Sub New(ByVal i_stb As StatusBar)
        MyBase.new()
        Try
            ''check xem có đủ 1 panel hay không
            Dim v_i As Object = i_stb.Panels(0)
            m_stb = i_stb
        Catch v_e As Exception
            Debug.Assert(False, "Phải có 1 Panel trên StatusBar thì mới làm được - Csung")
            Throw v_e
        End Try
    End Sub

    Public Sub addTextBox(ByVal i_txtBox As TextBox)
        If m_stb Is Nothing Then
            m_array_of_controls.Add(New DEControl(i_txtBox))
        Else
            m_array_of_controls.Add(New DEControl(i_txtBox, m_stb))
        End If
    End Sub

    Public Sub addComboBox(ByVal i_combo As ComboBox)
        If m_stb Is Nothing Then
            m_array_of_controls.Add(New DEControl(i_combo))
        Else
            m_array_of_controls.Add(New DEControl(i_combo, m_stb))
        End If
    End Sub

    Public Sub addControl(ByVal i_ctl As Control)
        m_array_of_controls.Add(New DEControl(i_ctl))
    End Sub


    Public Overrides Sub start_update()
        MyBase.start_update()
        enable_edit_controls()
        Me.display_status(C_UPDATE_STATUS_STRING)
        Me.display_info("")
    End Sub

    Public Overrides Sub start_insert_new()
        MyBase.start_insert_new()
        enable_edit_controls()
        Me.display_status(C_INSERT_STATUS_STRING)
        Me.display_info("")
    End Sub

    Public Overrides Sub go_to_view_mode()
        MyBase.go_to_view_mode()
        disable_edit_controls()
        Me.display_status(C_VIEW_STATUS_STRING)
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Chỉ dùng riêng cho handler những 
    ''' </summary>
    ''' <param name="i_keydown_handler"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/17/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub set_form_event_handler(ByVal i_keydown_handler As KeyEventHandler, _
                                      ByVal i_closing_handler As System.ComponentModel.CancelEventHandler)
        Dim v_frm_form As Form = m_stb.FindForm()
        v_frm_form.KeyPreview = True
        AddHandler v_frm_form.KeyDown, i_keydown_handler
        AddHandler v_frm_form.Closing, i_closing_handler
    End Sub

    Public Sub display_info(ByVal i_str_info_msg As String)
        m_stb.Panels(0).Text = i_str_info_msg
    End Sub

    Public Sub display_status(ByVal i_str_status As String)
        m_stb.Panels(0).Text = i_str_status
    End Sub

    Public ReadOnly Property form_in_control() As Form
        Get
            Return m_stb.FindForm()
        End Get
    End Property
#End Region

#Region "PRIVATE"
    Protected Sub disable_edit_controls()
        Dim v_i As Integer
        For v_i = 0 To m_array_of_controls.Count - 1
            Dim v_de_control As DEControl = CType(m_array_of_controls(v_i), DEControl)
            v_de_control.Enabled = False
        Next
    End Sub

    Protected Sub enable_edit_controls()
        Dim v_i As Integer
        For v_i = 0 To m_array_of_controls.Count - 1
            Dim v_de_control As DEControl = CType(m_array_of_controls(v_i), DEControl)
            v_de_control.Enabled = True
        Next
    End Sub
#End Region

#Region "Inner Classes"
    Private Class DEControl

        Private m_de_smart_textbox As DESmartTextBox
        Private m_de_smart_combo As DESmartCombo
        Private m_control As Control
        Private m_enabled As Boolean

        Public Sub New(ByVal i_txtBox As TextBox)
            m_de_smart_textbox = New DESmartTextBox(i_txtBox)
        End Sub

        Public Sub New(ByVal i_txtBox As TextBox, ByVal i_stb As StatusBar)
            m_de_smart_textbox = New DESmartTextBox(i_txtBox, New DENotifier(i_stb))
        End Sub

        Public Sub New(ByVal i_combo As ComboBox)
            m_de_smart_combo = New DESmartCombo(i_combo)
        End Sub

        Public Sub New(ByVal i_combo As ComboBox, ByVal i_stb As StatusBar)
            m_de_smart_combo = New DESmartCombo(i_combo, New DENotifier(i_stb))
        End Sub

        Public Sub New(ByVal i_ctl As Control)
            m_control = i_ctl
        End Sub

        Public Property Enabled() As Boolean
            Get
                Return m_enabled
            End Get
            Set(ByVal Value As Boolean)
                m_enabled = False
                If Not m_de_smart_combo Is Nothing Then
                    m_de_smart_combo.Enabled = Value
                ElseIf Not m_de_smart_textbox Is Nothing Then
                    m_de_smart_textbox.Enabled = Value
                ElseIf Not m_control Is Nothing Then
                    m_control.Enabled = Value
                End If
            End Set
        End Property
    End Class

#End Region




End Class


Public Class DENotifier
    Implements I_DEStateNotifier
    Private m_stb As StatusBar
    Public Sub New(ByVal i_stb As StatusBar)
        m_stb = i_stb
    End Sub
    Public Sub displayNotifier() Implements I_DEStateNotifier.displayNotifier
        m_stb.Panels(0).Text = "Phải chuyển sang mode SỬA để sửa dữ liệu"
    End Sub
End Class